﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IJSRuntime JS
@inject IDataCacheService _dataCacheService
@implements IDisposable


@if (string.IsNullOrWhiteSpace(Model.BasicInfor.Name) == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@Model.BasicInfor.Name" Description="@Model.BasicInfor.PersonalSignature" />
}
@if (_dataCacheService.ThemeSetting.IsTransparent)
{
    @if (string.IsNullOrWhiteSpace(Model.BasicInfor.SBgImage) == false)
    {
        <div id="web_bg" style="background-image: url(@Model.BasicInfor.SBgImage)" class="d-none bg-d-lg-block bgimage-ison"></div>
    }
    @if (string.IsNullOrWhiteSpace(Model.BasicInfor.MBgImage) == false)
    {
        <div id="web_bg" style="background-image: url(@Model.BasicInfor.MBgImage)" class=" d-none d-sm-block bgimage-ison"></div>
    }
}

@if (isNotFound)
{
    <NotFoundCard>
        <h1 class="text-center title-large">找不到这个用户哦......</h1>
        <div class="text-center">有点可怕呢，可以试试联系管理员</div>
    </NotFoundCard>
}
else
{
    @if (_dataCacheService.PersonalSpaceDataCatche.Check(Id) || isReady)
    {

        <div class="row">
            <div class="col-12 col-md-4">
                <CnGalWebSite.Shared.MasaComponent.PC.Users.MainInforCard Main Model="Model.BasicInfor" />
                 <CnGalWebSite.Shared.MasaComponent.PC.Users.BasicInforCard Model="Model" />
                 @if (_dataCacheService.IsApp == false)
                {
                    <CnGalWebSite.Shared.MasaComponent.PC.Users.CertificationCard Model="Model" />
                }
            </div>
            <div class="col-12 col-md-8">
                <div>
                    <MDCard Class="mb-4">
                        <MTabs Value="TabIndex" IconsAndText Grow CenterActive ValueChanged="TabValueChanged">

                             <MTab Value="1">
                                 首页<MIcon>mdi-home</MIcon>
                             </MTab>
                             <MTab Value="2">
                                 编辑记录<MIcon>mdi-script-text-outline </MIcon>
                             </MTab>
                             @if (Model.BasicInfor.ArticleCount != 0)
                            {
                                <MTab Value="3">
                                    文章<MIcon>mdi-newspaper-variant-multiple-outline</MIcon>
                                </MTab>
                            }
                            @if (Model.BasicInfor.VideoCount != 0)
                            {
                                <MTab Value="7">
                                    视频<MIcon>mdi-animation-play</MIcon>
                                </MTab>
                            }
                            @if (Model.IsCurrentUser || Model.IsShowGameRecord)
                            {
                                <MTab Value="4">
                                    游玩记录<MIcon>mdi-gamepad-square-outline </MIcon>
                                </MTab>
                            }
                            @if (Model.IsCurrentUser || Model.IsShowFavorites)
                            {
                                <MTab Value="11">
                                    收藏夹<MIcon>mdi-heart-multiple </MIcon>
                                </MTab>
                            }

                            <MTab Value="6">
                                周边<MIcon>mdi-basket-outline </MIcon>
                            </MTab>
                        </MTabs>
                    </MDCard>

                    <MTabsItems @bind-Value="TabIndex">
                        <MTabItem Value="1">
                            @if (_dataCacheService.IsApp)
                            {
                                <CnGalWebSite.Shared.MasaComponent.PC.Users.CertificationCard Model="Model" />
                            }
                            @if (Model.IsCurrentUser || string.IsNullOrWhiteSpace(Model.MainPageContext) == false)
                            {
                                <div class="mb-4">
                                    <CnGalWebSite.Components.Containers.MasaTitleContainer Title="个人介绍" Icon="@IconType.Home.ToIconString()">
                                         <ChildContent>
                                             <CnGalWebSite.Shared.MasaComponent.Shared.Components.Markdown.MarkdownViewTip Text="@Model.MainPageContext" />
                                         </ChildContent>
                                         <ButtonContent>
                                             @if (Model.IsCurrentUser)
                                            {
                                                <CnGalWebSite.Components.Buttons.MasaButton Fab Icon="@IconType.Edit.ToIconString()" Tooltip="编辑个人介绍" OnClick="EditMainPage" />

                                            }
                                        </ButtonContent>
                                    </CnGalWebSite.Components.Containers.MasaTitleContainer>

                                </div>
                            }

                            <div class="mb-4">
                                <CnGalWebSite.Components.Containers.MasaTitleContainer Title="签到记录" Icon="@IconType.DeterminedTime.ToIconString()" Collapable>
                                 <CnGalWebSite.Shared.MasaComponent.Shared.Components.Charts.MasaHeatMapChart Type="UserHeatMapType.SignInDays" Id="@Model.Id" />
                             </CnGalWebSite.Components.Containers.MasaTitleContainer>

                         </div>
                         <div class="mb-4">
                             <CnGalWebSite.Components.Containers.MasaTitleContainer Title="编辑概览" Icon="@IconType.Edit.ToIconString()" Collapable>
                                 <CnGalWebSite.Shared.MasaComponent.Shared.Components.Charts.MasaHeatMapChart Type="UserHeatMapType.EditRecords" ShowScrollBar Id="@Model.Id" />
                                 </CnGalWebSite.Components.Containers.MasaTitleContainer>
                             </div>


                             <CnGalWebSite.Shared.MasaComponent.PC.Users.DataOverviewCard Model="Model" />
                             <div>
                                 @if (Model.CanComment)
                                {

                                    <CnGalWebSite.Shared.MasaComponent.PC.Comments.CommentTip CanComment="Model.CanComment" CanDeleted="CanDeleted" ObjectId="@Model.Id" Type="CommentType.CommentUser" />
                                }
                            </div>

                        </MTabItem>
                        <MTabItem Value="2">
                            <CnGalWebSite.Shared.MasaComponent.PC.Users.ExaminesCard Id="@Model.Id" />
                        </MTabItem>
                        @if (Model.BasicInfor.ArticleCount != 0)
                        {
                            <MTabItem Value="3">

                                <CnGalWebSite.Shared.MasaComponent.PC.Users.ArticlesCard Id="@Model.Id" />

                            </MTabItem>
                        }

                        @if (Model.BasicInfor.VideoCount != 0)
                        {
                            <MTabItem Value="7">

                                <CnGalWebSite.Shared.MasaComponent.PC.Users.VideosCard Id="@Model.Id" />

                            </MTabItem>
                        }

                        @if (Model.IsCurrentUser || Model.IsShowGameRecord)
                        {
                            <MTabItem Value="4">
                                <CnGalWebSite.Shared.MasaComponent.PC.Users.UserPlayedGameListView SteamId="@Model.SteamId" UserId="@Model.Id" IsCurrentUser="Model.IsCurrentUser"></CnGalWebSite.Shared.MasaComponent.PC.Users.UserPlayedGameListView>
                            </MTabItem>
                        }


                        @if (Model.IsCurrentUser || Model.IsShowFavorites)
                        {
                            <MTabItem Value="11">
                                <FavoriteFoldersViewTip UserId="@Model.Id" IsCurrentUser="Model.IsCurrentUser"></FavoriteFoldersViewTip>
                            </MTabItem>
                        }

                        <MTabItem Value="6">
                            <UserOwnedPeripheryListView UserId="@Model.Id" />
                        </MTabItem>
                    </MTabsItems>

                </div>
            </div>
        </div>

    }
    else
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page />
    }


}

<CnGalWebSite.Shared.MasaComponent.Shared.Components.TrackEvent DataType="TrackEventDataType.User" EventType="TrackEventType.View" ObjectId="@Model.Id"
                                                                ObjectName="@Model.BasicInfor.Name" />
@if (isFirstLoad == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.NavLoadingCard IsLoading="isLoading" />
}

@code {

    [Parameter]
    public string Id { get; set; }

    public StringNumber TabIndex { get; set; } = 0;

    public string CurrentUserRank { get; set; }
    public int currentPage { get; set; } = 1;
    public string NoticeCard { get; set; } = "";
    bool isReady = false;
    bool isNotFound = false;
    bool isLoading = false;
    bool isFirstLoad = true;

    public PersonalSpaceViewModel Model { get; set; } = new PersonalSpaceViewModel();

    public PagedSortedAndFilterInput PlayedGameInput { get; set; } = new PagedSortedAndFilterInput();


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    public bool IsAdmin { get; set; } = false;
    public bool CanDeleted { get; set; }

    string LastLookName = "";
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            NavigationManager.LocationChanged -= LocationChanged;
            NavigationManager.LocationChanged += LocationChanged;
            isFirstLoad = false;
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await Refresh();
    }

    protected async Task Refresh()
    {

        try
        {
            TabIndex = _dataCacheService.SpaceViewCache.TabIndex;

            var userState = await authenticationStateTask;
            var user = userState.User;

            //获取用户基本信息
            Model = await _dataCacheService.PersonalSpaceDataCatche.GetCache(Id);

            if (Model.IsCurrentUser)
            {
                CurrentUserRank = "本人";
                CanDeleted = true;
            }
            if (user.IsInRole("Admin"))
            {
                IsAdmin = true;
                CanDeleted = true;
            }


            isReady = true;
            StateHasChanged();

        }
        catch (Exception exc)
        {
            isNotFound = true;
            isReady = true;
            StateHasChanged();
        }
    }


    private void EditMainPage()
    {
        NavigationManager.NavigateTo("/space/editmainpage/" + Model.Id);

    }

    public void TabValueChanged(StringNumber index)
    {
        _dataCacheService.SpaceViewCache.TabIndex = index.ToInt32();
        TabIndex = index;

    }

    public async void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (Id != Model.Id)
        {
            if (_dataCacheService.PersonalSpaceDataCatche.Check(Id) == false)
            {
                isLoading = true;
                StateHasChanged();
                await Refresh();
                isLoading = false;
                StateHasChanged();
            }
            else
            {
                await Refresh();
                StateHasChanged();
            }

        }
    }

    #region 释放实例

    public void Dispose()
    {

        NavigationManager.LocationChanged -= LocationChanged;

        GC.SuppressFinalize(this);
    }
    #endregion
}
